﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using loginSQL.DataView;
using MySql.Data;
using loginSQL.SQLHelper;
using loginSQL.Window;
using loginSQL.Pages;
using loginSQL.Util.Network;
using loginSQL.Window.PageEdit;

namespace loginSQL
{
    public partial class LoginScreen : Form
    {

        delegate void loginsuccessDelegate();
        delegate void loginFailedDelegate();
        private DBHelper myDBHelper;
        private string username;
        
        public LoginScreen()
        {
            InitializeComponent();
            //myDBHelper = new DBHelper(); 
            MySQLSettings settings = SaveLoadConfig.Load();
            Constants.serverName = settings.server;
            Constants.port = settings.port;
            Constants.usernameSQL = settings.username;
            Constants.passwordSQL = settings.password;
            Constants.databaseName = settings.databaseName;

            
            //combobox_filter.SelectedIndex = 0;
        }

        private void initCekKoneksi()
        {
            bool isServerLokalOnline = DBHelper.isDatabaseAvailable(new MySQLSettings(Constants.serverName, Constants.port, Constants.usernameSQL, Constants.passwordSQL, Constants.databaseName));
            bool isServerPusatOnline = PingUtility.tryPing(Constants.server_pusat);

            if (isServerLokalOnline)
            {
                pic_server_lokal.Image = new Bitmap(Properties.Resources.online);
            }
            else
            {
                pic_server_lokal.Image = new Bitmap(Properties.Resources.offline);
            }

            if (isServerPusatOnline)
            {
                pic_server_pusat.Image = new Bitmap(Properties.Resources.online);
            }
            else
            {
                pic_server_pusat.Image = new Bitmap(Properties.Resources.offline);
            }
        }

        private void loginsuccess()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new loginsuccessDelegate(loginsuccess));
                return;
            }
            this.panel_success.BringToFront();
            this.panel_success.Dock = DockStyle.Fill;
            this.username = this.txt_username.Text;
            this.label_welcome.Text = "Selamat Datang " + this.txt_username.Text + "!";
        }

        private void bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(1000);
            string username = this.txt_username.Text;
            string password = this.txt_password.Text;
            if (myDBHelper.doLogin(username,password))
            {
                loginsuccess();
            }
            else
            {
                loginFailed();
            }
            
        }

        private void loginFailed()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new loginFailedDelegate(loginFailed));
                return;
            }
            this.panel_login.BringToFront();
            this.panel_login.Dock = DockStyle.Fill;
            MessageBox.Show("Error Logging in: Check Your Username And Password");
        }

        private void btn_login_Click_1(object sender, EventArgs e)
        {
            myDBHelper = new DBHelper();
            initCekKoneksi();
            this.panel_loading.BringToFront();
            this.panel_loading.Dock = DockStyle.Fill;
            bgworker.RunWorkerAsync();
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string query = this.txt_search.Text;
            searchResult sr = new searchResult(query,DBHelper.FILTER_ALL);
            sr.Show();
        }

        private void txt_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                btn_login_Click_1(sender, e);
            }
        }

        private void txt_search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                btn_search_Click(sender, e);
            }
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void sQLSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SQLSetting sqlsetting = new SQLSetting();
            sqlsetting.Show();
        }

        private void label7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Bantuan");
        }

        private void panel12_Click(object sender, EventArgs e)
        {
            BookEdit bookedit = new BookEdit(Properties.Resources.collectionsFilename);
            bookedit.Show();
        }
    }
}
